<?php
/**
 *
 * @notes:
 * @author:     Jokenzhang
 * @time:       2020/12/22  16:07
 * @version:    V20201222.1.1
 *
 */

namespace Modules\Admin\Service;


use Modules\Admin\Entities\Module;

class ModuleService
{
    public function updateCache()
    {
        \DB::table('modules')->truncate();
        $modules = \HDModule::getModulesLists();
        foreach ($modules as $module) {
            $data = [
                'title' => $module['title'],
                'name' => $module['name'],
                'is_default' => 0,
                'front_access' => $this->frontAccess($module)
            ];
            Module::query()->create($data);
        }
        return true;
    }

    protected function frontAccess($module): bool
    {
        $class = 'Modules\\' . $module['name'] . '\Http\Controllers\HomeController';
        return class_exists($class) && method_exists($class, 'index');
    }

}
